package com.company.book_store.dao;
import com.company.book_store.pojo.User;
import com.company.book_store.util.MD5Utils;
import com.company.book_store.util.jdbcutils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class UserdaoImpl implements Iuserdao{
    private Connection con=null;
    @Override
    public User selectByIdandpsw(User user) {
        con= jdbcutils.getConnection();
        String sql="select * from user where username=? and password=?";
        PreparedStatement pst=null;
        ResultSet set=null;
        try {
            pst = con.prepareStatement(sql);
            pst.setString(1,user.getUsername());
            pst.setString(2, MD5Utils.stringToMD5(user.getPassword()));
            set=pst.executeQuery();
            User res=null;
            if(set.next())
            {
                String username=set.getString("username");
                String password=set.getString("password");
                String email=set.getString("email");
                String phone=set.getString("phone");
                String name=set.getString("name");
                res=new User(username,password,email,phone,name);
            }
            return res;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            jdbcutils.close(con,pst,set);
        }
        return null;
    }

    @Override
    public int insertUser(User user) {
        con= jdbcutils.getConnection();
        String sql="insert into user value(?,?,?,?,?)";
        PreparedStatement pst=null;
        ResultSet set=null;
        try {
            pst=con.prepareStatement(sql);
            pst.setString(1,user.getUsername());
            pst.setString(2,MD5Utils.stringToMD5(user.getPassword()));
            pst.setString(3,user.getEmail());
            pst.setString(4,user.getPhone());
            pst.setString(5,user.getName());
            return pst.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            jdbcutils.close(con,pst,set);
        }
        return 0;
    }

    @Override
    public User selectByusername(User user) {
        con= jdbcutils.getConnection();
        String sql="select * from user where username=?";
        PreparedStatement pst=null;
        ResultSet set=null;
        try {
            pst = con.prepareStatement(sql);
            pst.setString(1,user.getUsername());
            set=pst.executeQuery();
            User res=null;
            if(set.next())
            {
                String username=set.getString("username");
                String password=set.getString("password");
                String email=set.getString("email");
                String phone=set.getString("phone");
                String name=set.getString("name");
                res=new User(username,password,email,phone,name);
            }
            return res;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            jdbcutils.close(con,pst,set);
        }
        return null;
    }

    @Override
    public User selectByaccount(String str) {
        con= jdbcutils.getConnection();
        String sql="select * from user where username=?";
        PreparedStatement pst=null;
        ResultSet set=null;
        try {
            pst = con.prepareStatement(sql);
            pst.setString(1,str);
            set=pst.executeQuery();
            User res=null;
            if(set.next())
            {
                String username=set.getString("username");
                String password=set.getString("password");
                String email=set.getString("email");
                String phone=set.getString("phone");
                String name=set.getString("name");
                res=new User(username,password,email,phone,name);
            }
            return res;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            jdbcutils.close(con,pst,set);
        }
        return null;
    }

    @Override
    public List<User> selectall() {
        con=jdbcutils.getConnection();
        PreparedStatement pst=null;
        ResultSet set=null;
        String sql="select * from user";
        try {
            List<User>list=new ArrayList<>();
            pst=con.prepareStatement(sql);
            set=pst.executeQuery();
            while(set.next())
            {
                String username=set.getString("username");
                String password=set.getString("password");
                String email=set.getString("email");
                String phone=set.getString("phone");
                String name=set.getString("name");
                User user=new User(username,password,email,phone,name);
                list.add(user);
            }
            return list;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateuser(User user) {
        con=jdbcutils.getConnection();
        PreparedStatement pst=null;
        ResultSet set=null;
        String sql="update user set email=?,phone=?,name=? where username=?";
        try {
            pst=con.prepareStatement(sql);

            pst.setString(1,user.getEmail());
            pst.setString(2,user.getPhone());
            pst.setString(3,user.getName());
            pst.setString(4,user.getUsername());
            return pst.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }
//    public static void main(String[] args) {
//        User user=new User("admin","147963",null,null,null);
//        Iuserdao iuserdao=new UserdaoImpl();
////        List<User>list=iuserdao.selectall();
////        for(User user:list)
////        {
////            System.out.println(user);
////        }
//        System.out.println(iuserdao.updateuser(user));
//    }
}
